#include "Mario.h"

LPDIRECT3DTEXTURE9 Mario::_marioImage;

Mario::Mario(int level, int left, int top, CScreen *scr,QuadTree *q,SOUND *sManager)
{
	vX=MARIO_NORMAL_SPEED;
	_typeObj=1;
	_layer=1;
	_isActive=true;
	_kind=0;
	_quadTree=q;
	_pRECT=new RECT();
	_pRECT->top=top;
	_level=level;
	if (_level==0)
	{
		_pRECT->left=left+6;
		_pRECT->right=_pRECT->left+MARIO_SMALL_W-16;
		_pRECT->bottom=_pRECT->top+MARIO_SMALL_H;
		_pSprite=new CSprites(_marioImage,false,0,2,0,MARIO_SMALL_W,MARIO_SMALL_H,left,_pRECT->bottom,4000,90);
	}
	if (_level==1)
	{
		_pRECT->left=left+11;
		_pRECT->right=_pRECT->left+MARIO_BIG_W-21;
		_pRECT->bottom=_pRECT->top+MARIO_BIG_H;
		_pSprite=new CSprites(_marioImage,false,0,2,MARIO_SMALL_H*2,MARIO_BIG_W,MARIO_BIG_H,left,_pRECT->bottom,4000,90);
	}	
	if (_level==2)
	{
		_pRECT->left=left+11;
		_pRECT->right=_pRECT->left+MARIO_BIG_W-21;
		_pRECT->bottom=_pRECT->top+MARIO_BIG_H;
		_pSprite=new CSprites(_marioImage,false,0,2,MARIO_SMALL_H*2+MARIO_BIG_H*2,MARIO_BIG_W,MARIO_BIG_H,left,_pRECT->bottom,4000,90);
	}
	_pSprite->_enable=false;
	_isUpDown=false;
	_justChangeLevel=true;
	_justMove=false;
	_screen=scr;
	_direction=Direction::Right;
	_status=0;
	_id=countObj++;
	_pSoundManager=sManager;
}

void Mario::ChangeDirectory(bool isJump)
{
	switch(_status)
	{
	case 0://stand
		_pSprite->_index=0;
		break;
	case 1://jump
		if(_level==0)
			_pSprite->_index=2;
		else
			_pSprite->_index=3;
		break;
	case 2:
		_pSprite->_width=MARIO_SMALL_W;
		_pSprite->_index=3;
		_pSprite->_row=0;
		_pSprite->_interval=0;
		break;
	}
	if(!isJump)
		if(_direction==Direction::Letf)
			_pSprite->_row+=_pSprite->_height;
		else
			_pSprite->_row-=_pSprite->_height;
}
void Mario::ObjGetColliton(int kindColltion)
{
	if((_status==0&&!_justChangeLevel)||(_status==1&&isUp&&!_justChangeLevel))
	{
		if((_level < 2 && kindColltion>0)||(_level >=0 && kindColltion<0))
			_level+=kindColltion;
		if(kindColltion>0)
			_pSoundManager->Play("LevelUp",false);
		else
		{
			if(_level>=0)
				_pSoundManager->Play("BonusAppear_LevelDown",false);
			else
			{
			_pSoundManager->Play("Dying",false);
				_status=2;
				_isUpDown = true ;
				_deltaY = 15;
				isUp=true;
				_isSuper=false;
				ChangeDirectory(true);
			}
		}
		switch(_level)
		{
		case 0:
			if(_isUpDown==false)
				_pSprite->_index=0;
			else
				_pSprite->_index=2;
			if(_direction==Direction::Letf)
			{
				_pSprite->_row=MARIO_SMALL_H;
			}
			else
			{
				_pSprite->_row=0;
			}
			_pSprite->_count=2;
			_pSprite->_width=MARIO_SMALL_W;
			_pSprite->_height=MARIO_SMALL_H;
			_pSprite->_top+=MARIO_BIG_H-MARIO_SMALL_H;
			_pRECT->top+=MARIO_BIG_H-MARIO_SMALL_H;
			break;
		case 1:
			if(_isUpDown==false)
				_pSprite->_index=0;
			else
				_pSprite->_index=3;
			if(_direction==Direction::Right)
			{
				_pSprite->_row=MARIO_SMALL_H*2;
			}
			else
			{
				_pSprite->_row=MARIO_SMALL_H*2+MARIO_BIG_H;
			}
			_pSprite->_count=3;
			_pSprite->_width=MARIO_BIG_W;
			_pSprite->_height=MARIO_BIG_H;
			if(kindColltion==1)
			{
				_pSprite->_top-=MARIO_BIG_H-MARIO_SMALL_H;
				_pRECT->right=_pRECT->left+MARIO_BIG_W-8-6;
				_pRECT->top-=MARIO_BIG_H-MARIO_SMALL_H;
			}
			break;
		case 2:
			if(_isUpDown==false)
				_pSprite->_index=0;
			else
				_pSprite->_index=3;
			if(_direction==Direction::Right)
			{
				_pSprite->_row=MARIO_SMALL_H*2+MARIO_BIG_H*2;
			}
			else
			{
				_pSprite->_row=MARIO_SMALL_H*2+MARIO_BIG_H*3;
			}
			_pSprite->_count=3;
			_pSprite->_width=MARIO_BIG_W;
			_pSprite->_height=MARIO_BIG_H;
			break;
		}
		_justChangeLevel=true;
		_pSprite->_rootTime=GetTickCount();
	}
	
}
void Mario::Jump()
{
	if(_isUpDown&&_status==1)
	{
		if(isUp)
		{
			_deltaY--;
			_deltaY=CheckUpDown(true);
			_pSprite->_top-=_deltaY;
			_pRECT->top-=_deltaY;
			_pRECT->bottom-=_deltaY;
		}
		else
		{
			if(_deltaY<10)
				_deltaY++;
			_deltaY=CheckUpDown(false);
			_pSprite->_top+=_deltaY;
			_pRECT->top+=_deltaY;
			_pRECT->bottom+=_deltaY;
			if(_pSprite->_top>=WINDOW_HEIGHT||_deltaY==0)
			{
				if(_deltaY==0)
				{
					_status=0;
					_isUpDown=false;
					if(_typeObj==1)
						ChangeDirectory(true);
				}
				else
				{
					_status=2;
					if(_typeObj==1)
					{
						_isUpDown = true ;
						_deltaY = 15;
						isUp=true;
						_isSuper=false;
						_pSprite->_top-=10;
						_pRECT->top-=10;
						_pRECT->bottom-=10;
						ChangeDirectory(true);
						_pSoundManager->Play("Dying",false);
					}
			}
				return;
			}
		}
		if(_deltaY<=0)
		{
			isUp=false;
	
		}
	}
}

void Mario::Die()
{

	if(_isUpDown&&_status==2)//Died
	{
		if(isUp)
		{
			_deltaY--;
			_pSprite->_top-=_deltaY;
			_pRECT->top-=_deltaY;
			_pRECT->bottom-=_deltaY;
		}
		else
		{
			_deltaY++;
			_pSprite->_top+=_deltaY;
			_pRECT->top+=_deltaY;
			_pRECT->bottom+=_deltaY;
			if(_pSprite->_top>=WINDOW_HEIGHT)
			{
				_isUpDown=false;
				return;
			}
		}
		if(_deltaY<=0)
			isUp=false;
	}
}
int Mario::Move()
{
	if(GetTickCount()-lastTime<DELTA_TIME)
		return 0;
	lastTime=GetTickCount();
	if(_status!=2)//'s not died yet.
	{
		int DeltaX=CheckLeftRight();
		if(_pRECT->left-_screen->Camera.left>=WINDOW_WIDTH/2-20
			&&_pRECT->left-_screen->Camera.left<=WINDOW_WIDTH/2+20)
		{
			_screen->MoveCamera(DeltaX);
		}
		_pRECT->left+=DeltaX;
		_pRECT->right+=DeltaX;
		_pSprite->_left+=DeltaX;
		return DeltaX;
	}
	return 0;
}
void Mario::Draw()
{
	vector<Object*> result;
	_quadTree->CheckCollision(_quadTree->_pRoot,*_pRECT,_id,result);
	for(int i=0;i<result.size();i++)
	{
		if(_status==2)
			break;
		Object* obj=result[i];
		obj->Collision(this);
	}
	Jump();
	Die();
	if(_status==0&&!_quadTree->IsOnObject(_pRECT,_id))
	{
 		_status=1;
		ChangeDirectory(true);
		_isUpDown = true ; _deltaY = 0;
		Set_Up(false);
		_isSuper=false;
	}
	if(_status!=1)
		_pSprite->Draw(_screen->Camera.left,0,false);
	else
		_pSprite->Draw(_screen->Camera.left,2,false);
	if((GetTickCount()-_pSprite->_rootTime) > (DWORD)(_pSprite->_totalExistTime))
		_justChangeLevel=false;
}
void Mario::Collision(Object *Ob)
{
	Ob->Collision(this);
}

Mario::~Mario(void)
{
}
